<template>
  <el-card>
    <!-- inline=true 将el-form变为行内元素，多个el-form-item可以在一行排列 -->
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select v-model="categoryStore.c1Id" @change="handleFirst" :disabled="scene == 1">
          <el-option
            v-for="c1 in categoryStore.c1Arr"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="categoryStore.c2Id" @change="handleSecond" :disabled="scene == 1">
          <el-option
            v-for="c2 in categoryStore.c2Arr"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="categoryStore.c3Id" :disabled="scene == 1">
          <el-option
            v-for="c3 in categoryStore.c3Arr"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
//引入category小仓库
import useCategoryStore from '@/store/modules/category'
import { onMounted } from 'vue'
//引入attr的scene，使添加或修改属性时，三个下拉菜单都不能使用
defineProps(['scene'])

let categoryStore = useCategoryStore()
//获取一级分类数据
onMounted(() => {
  getC1()
})
//获取二级分类数据
const handleFirst = () => {
  //一级分类改变时要清空二三级数据
  categoryStore.c2Arr = []
  categoryStore.c2Id = ''
  categoryStore.c3Arr = []
  categoryStore.c3Id = ''
  if (categoryStore.c1Id) {
    getC2()
  }
}
//获取三级分类数据
const handleSecond = () => {
  //二级分类改变时要清空三级数据
  categoryStore.c3Arr = []
  categoryStore.c3Id = ''
  if (categoryStore.c2Id) {
    getC3()
  }
}
//通知仓库获取一级分类
const getC1 = () => {
  categoryStore.getC1()
}
//通知仓库获取二级分类
const getC2 = () => {
  categoryStore.getC2()
}
//通知仓库获取三级分类
const getC3 = () => {
  categoryStore.getC3()
}
</script>

<style scoped lang="scss"></style>
